package com.david.cloud.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.david.cloud.user.entity.Token;
import com.david.cloud.user.mapper.TokenMapper;
import com.david.cloud.user.service.ITokenService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author david
 * @since 2020-05-24
 */
@Service
public class TokenServiceImpl extends ServiceImpl<TokenMapper, Token> implements ITokenService {
    @Override
    public Token getByToken(String token) {
        List<Token> list = list(Wrappers.<Token>lambdaQuery().eq(Token::getToken, token));
        if (CollUtil.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public Token genToken(String email) {
        List<Token> list = list(Wrappers.<Token>lambdaQuery().eq(Token::getEmail, email));
        if (CollUtil.isNotEmpty(list)) {
            return list.get(0);
        } else {
            Token token = new Token();
            token.setEmail(email);
            token.setToken(UUID.fastUUID().toString());
            save(token);
            return token;
        }
    }
}
